import { dirname } from 'path';
import process from 'process'
import fs, { rmdir } from 'fs'
import path from 'path'
import jszip from 'jszip'
import { Asyncer, BuildBundle, CopyDir, DirForeach, FindProjectRoot, GetBuildTarget, GetMD5Path, MkDir, RmDir, ZipDir } from './src/utils.mjs';
import { BuildInfo } from './src/classes.mjs';
const __dirname = dirname(process.argv[1]);

const rootPath = FindProjectRoot();
if (!rootPath) {
    console.log(`项目文件夹错误`);
    process.exit(1);
}
console.log(`项目文件夹${rootPath}`);
let bundleList = process.argv.slice(2);

const buildPath = path.resolve(rootPath, "build/web-mobile");
const info = new BuildInfo();
if (fs.existsSync(path.resolve(buildPath, "build.info"))) {
    Object.assign(info, JSON.parse(fs.readFileSync(path.resolve(buildPath, `build.info`)).toString()));
    if (!info.mod) {
        console.warn("已打包，还没修改文件，请先执行build.mjs -m");
        process.exit(0);
    }
    if (!info.compress) {
        console.warn("注意：已打包，未进行压缩。");
    }
}
else {
    console.warn("还没打包，请先执行build.mjs -b [res/test]");
    process.exit(0);
}
const buildTarget = info.target;
const bundleRootPath = path.resolve(rootPath, `build/bundles-${buildTarget}`);
if (!fs.existsSync(bundleRootPath)) {
    await MkDir(bundleRootPath);
}

await BuildBundle(bundleRootPath, bundleList, info, true);
await Asyncer.awaitAllJobDone();
process.exit(0);